package org.szy.plugin.sys.service.impl;


import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.ProxyComponent;
import org.szy.core.auth.util.SaUtil;
import org.szy.core.common.exception.BaseException;
import org.szy.core.common.page.QueryPage;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.core.common.service.impl.BaseServiceImpl;
import org.szy.plugin.sys.entity.SysUser;
import org.szy.plugin.sys.mapper.SysUserMapper;
import org.szy.plugin.sys.param.SysUserQueryParam;
import org.szy.plugin.sys.service.SysOrgService;
import org.szy.plugin.sys.service.SysUserService;

import static org.szy.plugin.sys.entity.table.SysOrgTableDef.SYS_ORG;
import static org.szy.plugin.sys.entity.table.SysPersonTableDef.SYS_PERSON;
import static org.szy.plugin.sys.entity.table.SysUserTableDef.SYS_USER;

/**
 * 系统用户表Service接口实现类
 *
 * @author wangbuhao
 * @since 2023-06-27 09:56:03
 **/
@ProxyComponent
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Inject
    private SysUserMapper sysUserMapper;
    @Inject
    private SysOrgService sysOrgService;

    @Override
    public QueryWrapper qwByQuery(SysUserQueryParam param) {
        QueryWrapper qw = new QueryWrapper();
        if (StrUtil.isNotEmpty(param.getColumnField())) {
            qw.select(param.getColumnField().split(","));
        }

        if (StrUtil.isNotEmpty(param.getAccount())) qw.and(SYS_USER.ACCOUNT.eq(param.getAccount()));
        if (StrUtil.isNotEmpty(param.getAccountLike())) qw.and(SYS_USER.ACCOUNT.like(param.getAccountLike()));
        if (param.getStatus() != null) qw.and(SYS_USER.STATUS.eq(param.getStatus()));

        if (StrUtil.isNotEmpty(param.getOrgIdIn())) {
            // 关联人员
            qw.leftJoin(SYS_PERSON).on(SYS_PERSON.ID.eq(SYS_USER.PERSON_ID));
            // 人员管理机构
            qw.innerJoin(SYS_ORG).on(SYS_ORG.ID.eq(SYS_PERSON.ORG_ID));
            qw.and(SYS_ORG.PIDS.like(param.getOrgIdIn()).or(SYS_ORG.ID.eq(param.getOrgIdIn())));
        }
        return qw;
    }

    @Override
    public Page<SysUser> page(QueryPage<SysUser> page, SysUserQueryParam param) {
        QueryWrapper qw = qwByQuery(param);

        // 正序排序
        if (StrUtil.isNotEmpty(page.getAscField())) qw.orderBy(page.getAscField().split(","));
        // 倒叙排序
        if (StrUtil.isNotEmpty(page.getDescField())) {
            for (String desc : page.getDescField().split(",")) {
                qw.orderBy(new QueryColumn(desc).desc());
            }
        }

        return this.page(page, qw);
    }

    @Override
    public boolean add(SysUser entity) {
        String pwd = SaUtil.md5(entity.getPassword());
        entity.setPassword(pwd);
        return this.save(entity);
    }

    @Override
    public boolean modifyStatus(CommonStatusParam param) {
        SysUser entity = UpdateEntity.of(SysUser.class, param.getId());
        entity.setStatus(param.getStatus());
        return sysUserMapper.update(entity) > 0;
    }

    @Override
    public boolean modifyPwd(Long id, String password) {
        SysUser entity = UpdateEntity.of(SysUser.class, id);
        if (entity == null) throw new BaseException("用户无效");
        String pwd = SaUtil.md5(password);
        entity.setPassword(pwd);
        return sysUserMapper.update(entity) > 0;
    }

    @Override
    public SysUser getByAccount(String account) {
        return getOneByKAV("account", account);
    }

    @Override
    public void modifyAvatar(Long id, String avatar) {
        SysUser of = UpdateEntity.of(SysUser.class, id);
        of.setAvatar(avatar);
        sysUserMapper.update(of);
    }
}
